package com.xxx.store.service.impl;

import com.xxx.store.entity.Cart;
import com.xxx.store.mapper.CartMapper;
import com.xxx.store.mapper.ProductMapper;
import com.xxx.store.service.CartService;
import com.xxx.store.service.exception.AccessDeniedException;
import com.xxx.store.service.exception.CartNotFoundException;
import com.xxx.store.service.exception.InsertException;
import com.xxx.store.service.exception.UpdateException;
import com.xxx.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    //购物车业务层依赖于购物车持久层及商品持久层
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public void addToCart(Integer uid, Integer pid, Integer num, String username) {
        //查询当前要添加的商品在购物车中是否存在
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        Date date = new Date();
        if (result==null){
            Cart cart = new Cart();
            cart.setPid(pid);
            cart.setUid(uid);
            cart.setNum(num);
            cart.setPrice(productMapper.findById(pid).getPrice());
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            Integer insert = cartMapper.insert(cart);
            if (insert!=1){
                throw new InsertException("数据插入失败！");
            }
        }else {
            Integer integer = cartMapper.updateNumByCid(result.getCid(), num, username, date);
            if (integer!=1){
                throw new UpdateException("更新产生异常!");
            }
        }
    }

    @Override
    public List<CartVO> getCartByUid(Integer uid) {
        List<CartVO> list = cartMapper.findVOByUid(uid);
        return list;
    }

    @Override
    public Integer addNum(Integer cid, Integer uid,String username) {
        Cart cart = cartMapper.findByCid(cid);
        if (cart==null){
            throw new CartNotFoundException("数据不存在");
        }
        if(!cart.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num=cart.getNum()+1;
        Integer integer = cartMapper.updateNumByCid(cid,1, username, new Date());
        if (integer!=1){
            throw new UpdateException("更新数据产生异常");
        }
        return num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        Cart cart = cartMapper.findByCid(cid);
        if (cart==null){
            throw new CartNotFoundException("数据不存在");
        }
        if(!cart.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num=cart.getNum()-1;
        if (num<=0){
            return 0;
        }else {

            Integer integer = cartMapper.updateToReduceNumByCid(cid,1, username, new Date());
            if (integer!=1){
                throw new UpdateException("更新数据产生异常");
            }
            return num;
        }
    }

    @Override
    public List<CartVO> getCartByCid(Integer uid, Integer[] cids) {
        List<CartVO> list = cartMapper.findVOByCid(cids);
        Iterator<CartVO> iterator = list.iterator();
        while (iterator.hasNext()){
            CartVO cartVO = iterator.next();
            if (!cartVO.getUid().equals(uid)) iterator.remove();
        }
        return list;
    }
}
